<template>
    <div id="add-edit-dataMaintain">
        <el-dialog
            :title="titleCon"
            v-model="isUseDialog" 
            draggable 
            :close-on-click-modal="false" 
            width="769px" 
            :before-close="handleClose"
            :destroy-on-close="true"
            @close="clean"
        >
            <el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="auto" :rules="rules">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('4iZlKyTdZvFnmo01f8Onk')" prop="typeId">
                            <el-select filterable v-model="ruleForm.typeId" :placeholder="$t('h8Zy9JyDr67Qf6oalOhtb')">
                                <el-option
                                v-for="item in typeOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('n19BdGmJnaRdCbb3Qnmei')" prop="value">
                            <el-input v-model="ruleForm.value" :placeholder="$t('yswJbLhfKnJPk5mlbeNGd')" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('yHtfWyowlaKpfb5ytVDns')" prop="code">
                            <el-input v-model="ruleForm.code" :placeholder="$t('tveD7suzXctM4sRRi24j4')" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('j5WomRd9hpKPdnIuQrK0S')">
                            <el-switch
                                v-model="ruleForm.status"
                                class="ml-2"
                                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="$t('jJqL1X8ChQgp81e0Gpzm')">
                            <el-input type="textarea" v-model="ruleForm.remark" :placeholder="$t('98wJzI3lyj0Hk53nqIMpg')" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button @click="cancelUseClick">{{ $t('gJikJmcygxvlVId7NvzSu') }}</el-button>
                <el-button type="primary" :disabled="repeatDis" @click="submitForm(ruleFormRef)">{{ $t('hfPwhz0wXrlEiXoAqXs9W') }}</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive , computed , toRef , toRefs , toRaw , onMounted , watch } from 'vue'
import { FormInstance, FormRules , ElMessage, ElButton, ElCol, ElDatePicker, ElDialog, ElDivider, ElForm, ElFormItem, ElInput, ElOption, ElRadio, ElRow, ElSelect, ElTable, ElTableColumn } from 'element-plus'
import getTableData from '/@/utils/getTableData'
import { sysDictDataAdd , sysDictDataEdit , sysDictTypePage } from '/@/api/manage/manage'
import useCurrentInstance from '/@/utils/hook/useCurrentInstance';
const { proxy } = useCurrentInstance();
const $t = proxy.$t;
/*** 定义接口基础数据类型 */
interface formType {
    repeatDis: Boolean,
    ruleForm: ruleFormType,

}
/*** form 表单数据类型 */
type ruleFormType = {
    id: any,
    typeId: any,
    value: String,
    code: String,
    remark: String,
    status: Boolean,
}
export default defineComponent( {
    name: 'add-edit-dataMaintain',
    components: {},
    props: {
        isUseDialog:{
            type: Boolean,
            default: false,
        },
        repairInfo: {
            type: String,
            default: ''
        }
    },
    setup(props,ctx){
        const titleCon = ref()
        const ruleFormRef = ref()
        const typeOptions = ref()

        /*** state 引用接口定义基础数据初始值 */
        const state = reactive < formType > ({
            repeatDis: false,
            ruleForm: {} as ruleFormType
        })

        /*** 基础验证 rules */
        const rules = reactive < FormRules > ({
            /*** 放置验证 */
            typeId: [
				{ required: true, message: $t('bZPyXyuHrBoa_98QJvEc'), trigger: 'change' },
			],
            value: [
				{ required: true, message: $t('yswJbLhfKnJPk5mlbeNGd'), trigger: 'blur' },
			],
            code: [
				{ required: true, message: $t('nVrAcmUd_7IulKyI6IE'), trigger: 'blur' },
			],
        })

        /*** GetInfo 获取数据 */
        const GetInfo = async () => {
            try {
                let res:any = await sysDictTypePage()
                if(res.success){
                    typeOptions.value = res.data.rows 
                }
            } catch (error) {return}
        }

        /*** handleClose 关闭按钮点击事件 */
        const handleClose = () => {
            ctx.emit('cancelUseClick')
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            ctx.emit('cancelUseClick')
        }

        /*** submitForm 提交事件 */
        const submitForm = (formEl: FormInstance | undefined) => {
            if(!formEl) return
            formEl.validate( async (valid) => { 
                if(valid){
                    let setObj = toRaw(state.ruleForm)
                    let param = {
                        id : setObj.id,
                        typeId : setObj.typeId,
                        value : setObj.value,
                        code : setObj.code,
                        remark : setObj.remark,
                        status : setObj.status ? 0 : 1,
                    }
                    if(param.id === ''){
                        const { id , ...params} = param
                        let res:any = await sysDictDataAdd(params)
                        if(res.success){
                            state.repeatDis = true
                            ctx.emit('controlUseClick')
                            ctx.emit('updateData')
                        }else{
                            ctx.emit('controlUseClick','')
                        }
                    }else{
                        let res:any = await sysDictDataEdit(param)
                        if(res.success){
                            state.repeatDis = true
                            ctx.emit('controlUseClick')
                            ctx.emit('updateData')
                        }else{
                            ctx.emit('controlUseClick','')
                        }
                    }
                    return
                    state.repeatDis = true
                    ctx.emit('controlUseClick')
                }else{
                    console.log('error submit!')
					return false
                }
            })
        }

        /*** clean 清空数据 */
        const clean = () => {
            ruleFormRef.value.resetFields()
        }

        /*** 监听isUseDialog的变化 */
        watch(() => props.isUseDialog,(newVal) => {
            if(newVal){
                state.repeatDis = false
                GetInfo()
                if(props.repairInfo !== ''){
                    titleCon.value = $t('pAtGxrCkRSpku0DwjGyEk')
                    console.log(JSON.parse(props.repairInfo),'repairInfo')
                    state.ruleForm.id = JSON.parse(props.repairInfo).id
                    state.ruleForm.typeId = JSON.parse(props.repairInfo).typeId
                    state.ruleForm.value = JSON.parse(props.repairInfo).value
                    state.ruleForm.code = JSON.parse(props.repairInfo).code
                    state.ruleForm.remark = JSON.parse(props.repairInfo).remark
                    state.ruleForm.status = JSON.parse(props.repairInfo).status === 1 ? false : true
                }else{
                    titleCon.value = $t('byjccsglzWnFEuusQvL9Z')
                    state.ruleForm.id = ''
                    state.ruleForm.typeId = ''
                    state.ruleForm.value = ''
                    state.ruleForm.code = ''
                    state.ruleForm.remark = ''
                    state.ruleForm.status = true
                }
            }
        })

        /*** isUseDialog 响应弹窗的关闭  */
        const isUseDialog = computed(() => {
            return props.isUseDialog
        })

        return {
            handleClose,
            submitForm,
            cancelUseClick,
            clean,
            ruleFormRef,
            titleCon,
            ...toRefs(state),
            isUseDialog,
            rules,
            typeOptions,
        }
    },
} )
</script>
<style scoped lang="scss">
    :deep(.el-select){
        width: 100%;
    }
</style>